package br.edu.ufcg.dsc.domain.resource;

import java.util.ArrayList;
import java.util.List;

import br.edu.ufcg.dsc.domain.marc.MARCResource;

public class TechnicalReport {
	//Gerais
	private String title, author, contributor, cutter, source, format, 
				   date, description,  CDUClassification, publisherName, 
				   publisherPlace, language, country;
	//Especificos
	private String number;
	
	public TechnicalReport(MARCResource marc){
		setTitle(getAttribute(marc, "245", "a"));
		setAuthor(getAttribute(marc, "720", "a"));
		setNumber(getAttribute(marc, "020", "a"));
		setCutter(getAttribute(marc, "CUTTER", "X"));
		setContributor(getAttribute(marc, "720", "a"));
		setDescription(getAttribute(marc, "520", "a"));
		setSource(getAttribute(marc, "786", "a"));
		setFormat(getAttribute(marc, "856", "q"));
		setDate(getAttribute(marc, "260", "c"));
		setPublisherName(getAttribute(marc, "260", "b"));
		setPublisherPlace(getAttribute(marc, "PUBLISER_PLACE", "X"));
		setCDUClassification(getAttribute(marc, "CDU_CLASSIFICATION", "X"));
		setLanguage(getAttribute(marc, "LANGUAGE", "X"));
		setCountry(getAttribute(marc, "COUNTRY", "X"));		
	}

	public TechnicalReport() {}

	private List<String> getMultipleAttributes(MARCResource marc, String field,
			String subfield) {

		List<String> result = new ArrayList<String>();

		for (String key : marc.getDataFields().keySet())
			if (key.startsWith(field))
				for (String subkey : marc.getDataFields().get(key)
						.getSubfields().keySet())
					if (subkey.equals(subfield))
						result.add(marc.getDataFields().get(key)
								.getSubfields().get(subkey));

		return result;
	}

	private String getAttribute(MARCResource marc, String field, String subfield) {
		if (marc.getDataFields().get(field) == null)
			return "";
		for (String key : marc.getDataFields().keySet())
			if (key.startsWith(field))
				for (String subkey : marc.getDataFields().get(key)
						.getSubfields().keySet())
					if (subkey.equals(subfield))
						return marc.getDataFields().get(key).getSubfields().get(subkey);
		return "";
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getContributor() {
		return contributor;
	}

	public void setContributor(String contributor) {
		this.contributor = contributor;
	}

	public String getCutter() {
		return cutter;
	}

	public void setCutter(String cutter) {
		this.cutter = cutter;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getCDUClassification() {
		return CDUClassification;
	}

	public void setCDUClassification(String CDUClassification) {
		this.CDUClassification = CDUClassification;
	}

	public String getPublisherName() {
		return publisherName;
	}

	public void setPublisherName(String publisherName) {
		this.publisherName = publisherName;
	}

	public String getPublisherPlace() {
		return publisherPlace;
	}

	public void setPublisherPlace(String publisherPlace) {
		this.publisherPlace = publisherPlace;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
}
